/*
 * The MIT License
 * 
 * Copyright (c) 2008 Comcast Interactive Media
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package cim.sf.core.command;

import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;

import cim.sf.core.command.internal.EmptyInputStream;

/**
 * Passed to a <code>{@link Command}</code> with invocation context.
 * 
 * @author <a href="mailto:jeremy_volkman@cable.comcast.com">Jeremy Volkman</a>
 *
 */
public class CommandInvocation {

    private static final InputStream EMPTY_INPUT = new EmptyInputStream();
    
    /**
     * Output stream
     */
    public final PrintStream out;

    /**
     * Error stream
     */
    public final PrintStream err;

    /**
     * Input stream
     */
    public final InputStream in;

    /**
     * Command arguments
     */
    public final String[] args;

    private boolean hasInput;
    private File cwd;
    
    /**
     * Constructs a CommandInvocation object.
     * @param out Output stream
     * @param err Error stream
     * @param in Input stream
     * @param args Command arguments
     * <p>
     * if <code>in</code> is <code>null</code>, the <code>{@link #in}</code> field will be set to
     * an empty InputStream, and <code>{@link #hasInput()}</code> will return <code>false</code>.
     */
    public CommandInvocation(PrintStream out, PrintStream err, InputStream in, File cwd, String[] args) {
        this.out = out;
        this.err = err;
        this.in = in != null ? in : EMPTY_INPUT;
        this.args = args;
        this.cwd = cwd;
        hasInput = in != null;
    }

    /**
     * Returns whether or not this invocation contains input on the {@link #in} stream.
     * @return <tt>true</tt> if input exists.
     */
    public boolean hasInput() {
        return hasInput;
    }
    
    /**
     * If a command is invoked in such a way that it has an associated working directory (from a 
     * UNIX shell, for instance), this method will return that directory. Else, the method returns
     * <code>null</code>.
     * @return working directory.
     */
    public File getCwd() {
        return cwd;
    }
}
